import orderApi from "@/api/orderApi.js";

export default {
  data() {
    return {
      closeOrderShow: false,
      closeIndex: 0, // 关闭下标
      otherReasonsShow: false, // 其他原因
      otherReasonsActive: 0,
      otherReasons: ['收货地址或手机号码填错了','忘记支付密码/余额不足','无法正常支付','买错了/不想买了','其他原因'],
      otherReasonsParams: {
        orderAllId: '',
        cancelReason: '' 
      },
      cancelReason: '',
      pay_record_id: null, // 交易ID
      timer: null, // 未支付
      timer1: null, // 支付状态
      payDisabledType: false,
      payIndex: 0, // 当前支付订单下标
    }
  },
  beforeDestroy(){
    if (this.timer1) {
      clearInterval(this.timer1); 
    }
  },
  methods: {
    // 未支付取消订单
    _closeOrder(ord,index){
      this.closeIndex = index
      this.closeOrderShow = true // 弹出取消原因弹窗
      this.otherReasonsParams.orderAllId = ord.orderAllId
    },
    // 切换取消原因
    _otherReasonsChange(idx){
      this.otherReasonsActive = idx
      if(idx === 4){
        this.otherReasonsShow = true
      }
    },
    // 确定取消
    async _configClose(){
      if(this.otherReasonsActive === 4){
        this.otherReasonsParams.cancelReason = this.cancelReason
      }else{
        this.otherReasonsParams.cancelReason = this.otherReasons[this.otherReasonsActive]
      }
      orderApi.myCancelorder(this.otherReasonsParams).then(()=>{
        if(this.closeIndex || this.closeIndex === 0){ // 下标为列表页,无下标为详情页
          this.orderList[this.closeIndex].status = 2 // 刷新订单状态为已取消
        }else{
          this.orderDetails.status = 2
        }
        this.otherReasonsShow = false
        this.closeOrderShow = false
        uni.showToast({
          title: '取消成功',
          icon: 'none'
        })
      })
    },
    // 查看物流
    goLogistics(orderAllId){
      this.navigateTo({ url: `/pagesA/order/logisticsMode?orderAllId=${orderAllId}` })
    },
    // 确认收货
    async receipt(orderAllId,index){
      uni.showModal({
        title: '确认收到货了吗？',
        content: '为了保证你的售后权益，请收到商品确认无误后再确认收货',
        confirmColor: '#E44435',
        confirmText: '确认收货',
        cancelColor: '#969799',
        cancelText: '取消',
        success: ({ confirm, cancel }) => {
          if(confirm){
            orderApi.myConfirmReceiving({orderAllId}).then(()=>{
              if(index || index === 0){ // 下标为列表页,无下标为详情页
                this.orderList[index].status = 6 // 刷新订单状态为已收货
              }else{
                this.orderDetails.status = 6
              }
              uni.showToast({
                title: '收货成功',
                icon: 'none'
              })
            })
          }
        }
      })
    },
    // 立即支付
    _immediatelydPay(ord,index){
      uni.showModal({
        title: '付款提示',
        content: '支付后你的资金将进入对方账户，请确认对方信息后支付',
        confirmColor: '#E44435',
        confirmText: '立即支付',
        cancelColor: '#969799',
        cancelText: '取消',
        success: ({ confirm, cancel }) => {
          if(confirm){
            this.payDisabledType = true // 支付中禁用支付按钮
            this.payIndex = index
            this._playFn(ord.orderAllId)
          } 
        }
      })
    },
    _playFn(orderAllId){
      let _this = this
      orderApi.payTrade({orderAllId}).then(res => {
        if (res.code == 200) {
          wx.showLoading({
            title: '支付中'
          })
          _this.pay_record_id = res.data.outTradeNo
          let payRes = res.data.weChatRespVO
          wx.requestPayment({
            timeStamp: payRes.timeStamp,
            nonceStr: payRes.nonceStr,
            package: payRes.packageInfo,
            signType: payRes.signType,
            paySign: payRes.paySign,
            success (res_) {
              if (res_.errMsg == 'requestPayment:ok') { // 支付
                _this.payDisabledType = false
              }
            },
            fail (res_) {
              if (res_.errMsg == 'requestPayment:fail cancel') {
                console.log('取消支付');
              }
              if (_this.timer1) clearInterval(_this.timer1)
              _this.payDisabledType = false
              wx.hideLoading()
            }
          })
          _this.timer1 = setInterval(()=>{
            _this.paystatusFn()
          },1500)
        } else {
          _this.payDisabledType = false
        }
      })
    },
    // 查询支付状态
    async paystatusFn(){
      orderApi.queryPayStatus({outTradeNo:this.pay_record_id}).then(res => {
        if (res.code == 200&&res.data.status==2) { // 支付成功
          clearInterval(this.timer1)
          if(this.payIndex || this.payIndex === 0){ // 下标为列表页,无下标为详情页
            this.orderList[this.payIndex].status = 4 // 刷新订单状态为代发货
          }else{
            uni.showToast({
              title: '支付成功',
              icon: 'none'
            })
            this.orderDetails.status = 4
          }
        }
        if (res.code == 200 && res.data.status==3) { // 支付失败
          if (this.timer1) clearInterval(this.timer1)
        }
        wx.hideLoading()
      })
    },
    // 再次购买
    _againOrder(ord){
      // this.$store.commit('STAY_PAY_FORGOODS', goodsData)
      // this.navigateTo({url: '/pagesA/shoppingCar/confirmOrder'});
    },
    // 转换时间格式，五分钟倒计时处理
    _countDownmintus(data){
      console.log(data);
      let s;
      let hours = parseInt((data % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      let minutes = parseInt((data % (1000 * 60 * 60)) / (1000 * 60));
      let seconds = parseInt((data % (1000 * 60)) / 1000);
      s = (hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? ('0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds);
      return s;
    },
    // 跳转去售后详情
    _goAfersaleDetails(orderAfterSalesId){
      this.navigateTo({url: `/pagesA/order/myAftersaleDetails?orderAfterSalesId=${orderAfterSalesId}`});
    }
  }
}